package com.example.qxfw.ybzz.controller;
import com.example.qxfw.common.entity.qx.MsgMediumSmallScale;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.*;
import com.example.qxfw.common.entity.xt.*;
import com.example.qxfw.common.entity.zy.*;
import com.example.qxfw.rcyw.service.SmallScaleStationService;
import com.example.qxfw.ybzz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@ApiIgnore
@Api(tags = "预报制作-精细化预报制作")
@RestController
@CrossOrigin
@RequestMapping("/jxhbzz")
public class JxhybzzController {

    @Autowired
    private YbzzForecastService ybzzForecastService;

    @Autowired
    private SmallScaleStationService smallScaleStationService;

    @Autowired
    private YbzzForeCastFinalService ybzzForeCastFinalService;

    @Autowired
    private TownshipForecastService townshipForecastService;

    @Autowired
    private YbzzForecastshuzhiService ybzzForecastshuzhiService;

    @Autowired
    private MsgMediumSmallScaleService msgMediumSmallScaleService;

    @Autowired
    private YbzzForecastkeguandzService ybzzForecastkeguandzService;

    @Autowired
    private YbzzForecastdiffService ybzzForecastdiffService;

    @Autowired
    private YbzzConfigService ybzzConfigService;

    @Autowired
    private PermissionsService permissionsService;

    @Autowired
    private PermissionsRolesService permissionsRolesService;

    @Autowired
    private RolesUsersService rolesUsersService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private GridFileService gridFileService;

    @Autowired
    private EnviroStationService enviroStationService;

    @Autowired
    private TownshipStationService townshipStationService;

    @Autowired
    private TourStationsService tourStationsService;

    @Autowired
    private TrafficWeatherStaService trafficWeatherStaService;

    @Autowired
    private AirpolFcstQxService airpolFcstQxService;

    @Autowired
    private TourFcstService tourFcstService;

    @Autowired
    private TrafficFcstService trafficFcstService;

    //精细化预报制作

    @ApiOperation("获取预报时间")
    @GetMapping("/getForecastTime")
    public SysResult getForecastTime(Integer forecastType, String userName) {
        List<String> forecastTime = ybzzForecastService.getForecastTime(forecastType, userName);
        return SysResult.success(forecastTime);
    }

    @ApiOperation("获取日期和查询点")
    @GetMapping("/getDateCharAndNTimes")
    public SysResult getDateCharAndNTimes(String forecastTime) {
        List<YbzzForecast> ybzzForecasts = ybzzForecastService.getDateCharAndNTimes(forecastTime);
        return SysResult.success(ybzzForecasts);
    }

    @ApiOperation("查询预报数据")
    @GetMapping("/getForecast")
    public SysResult getForecast(String userName, String dateChar, Integer forecastType, Integer nTimes, String forecastTime, int[] unCheck) {
        List<YbzzForecast> ybzzForecasts = ybzzForecastService.listByParam(userName, dateChar, forecastType, nTimes, forecastTime);
        List<YbzzForecast> ybzzForecastList = ybzzForecasts.stream().map(
                i -> {
                    YbzzForecast bo = new YbzzForecast();
                    BeanUtils.copyProperties(i, bo);
                    for (int index : unCheck) {
                        switch (index) {
                            case 1:
                                bo.setTemperature(null);
                                break;
                            case 2:
                                bo.setRain(null);
                                break;
                            case 3:
                                bo.setCloudtotal(null);
                                break;
                            case 4:
                                bo.setWind(null);
                                bo.setWindd(null);
                                break;
                            case 5:
                                bo.setHumid(null);
                                break;
                        }
                    }
                    SmallScaleStation smallScaleStation = smallScaleStationService.getById(bo.getStationid());
                    bo.setStationName(smallScaleStation.getStationname());
                    bo.setLon(smallScaleStation.getLongitude());
                    bo.setLat(smallScaleStation.getLatitude());
                    return bo;
                }

        ).collect(Collectors.toList());
        return SysResult.success(ybzzForecastList);
    }

    @ApiOperation("获取省台格点最新时间")
    @GetMapping("/getProRecentTime")
    public SysResult getProRecentTime(){
        List<YbzzConfig> list = ybzzConfigService.getList();
        String timeStr = "";
        for (YbzzConfig ybzzConfig : list) {
            if (ybzzConfig.getElementtype().equals("省台最新时间")) {
                timeStr = ybzzConfig.getMappedpath();
                break;
            }
        }
        if (timeStr.length()==0){
            return SysResult.fail("获取最新时间失败");
        }
        return SysResult.success(20+timeStr.substring(0,2)+"-"+timeStr.substring(2,4)+"-"+timeStr.substring(4,6)+" "+timeStr.substring(6));
    }

    @ApiOperation("获取省台格点类型")
    @GetMapping("/getProType")
    public SysResult getProType(){
        List<YbzzConfig> list = ybzzConfigService.getList();
        list.removeIf(ybzzConfig -> !(ybzzConfig.getElementtype().equals("温度")
                || ybzzConfig.getElementtype().equals("降水")
                || ybzzConfig.getElementtype().equals("云量")
                || ybzzConfig.getElementtype().equals("风速")
                || ybzzConfig.getElementtype().equals("湿度")));
        return SysResult.success(list);
    }

    @ApiOperation("预报制作")
    @GetMapping("/ybzz")
    public SysResult ybzz(){
        return SysResult.success();
    }

    @ApiOperation("获取站点id")
    @PostMapping("/getYbzzForecastStationId")
    public SysResult getYbzzForecastStationId() {
        List<String> stationids = ybzzForecastService.getStationId();
        return SysResult.success(stationids);
    }

    @ApiOperation("获取")
    @GetMapping("/getYbzzForecastByType")
    public SysResult getYbzzForecastByType(String forecastTime, String forecastType) {
        List<YbzzForecast> ybzzForecasts = ybzzForecastService.getListByType(forecastTime, forecastType);
        return SysResult.success(ybzzForecasts);
    }

    @ApiOperation("更新")
    @PostMapping("/updateYbzzForecast")
    public SysResult updateYbzzForeCast(YbzzForecast ybzzForecast) {
        int rows = ybzzForecastService.update(ybzzForecast);
        return SysResult.success(rows);
    }

    @ApiOperation("删除")
    @GetMapping("/deleteYbzzForecast")
    public SysResult deleteYbzzForecast(String shuZhiDate, String forecastType, String strTimeChar, String stationList) {
        int rows = ybzzForecastService.delete(shuZhiDate, forecastType, strTimeChar, stationList);
        return SysResult.success(rows);
    }

    @ApiOperation("添加")
    @PostMapping("/saveYbzzForecast")
    public SysResult saveYbzzForecast(YbzzForecast ybzzForecast) {
        int rows = ybzzForecastService.save(ybzzForecast);
        return SysResult.success(rows);
    }

    @ApiOperation("更新")
    @PostMapping("/updateYbzzForecastFinal")
    public SysResult updateYbzzForeCastFinal(YbzzForecastFinal ybzzForecastFinal) {
        int rows = ybzzForeCastFinalService.update(ybzzForecastFinal);
        return SysResult.success(rows);
    }

    @ApiOperation("删除")
    @PostMapping("/deleteYbzzForecastFinal")
    public SysResult deleteYbzzForecastFinal(String shuZhiDate, String shuZhiEndDate, String intForecastType, String stationList) {
        int rows = ybzzForeCastFinalService.delete(shuZhiDate, shuZhiEndDate, intForecastType, stationList);
        return SysResult.success(rows);
    }

    @ApiOperation("添加")
    @PostMapping("/saveYbzzForecastFinal")
    public SysResult saveYbzzForecastFinal(YbzzForecastFinal ybzzForecastFinal) {
        int rows = ybzzForeCastFinalService.save(ybzzForecastFinal);
        return SysResult.success(rows);
    }

    @ApiOperation("获取乡镇预报数据")
    @GetMapping("/getTownshipForecast")
    public SysResult getTownshipForecast(String forecastTime, String strTimeChar, String stationID) {
        List<TownshipForecast> list = townshipForecastService.getList(forecastTime, strTimeChar, stationID);
        return SysResult.success(list);
    }

    @ApiOperation("更新乡镇预报数据")
    @PostMapping("/updateTownshipForecast")
    public SysResult updateTownshipForecast(TownshipForecast townshipForecast) {
        int rows = townshipForecastService.update(townshipForecast);
        return SysResult.success(rows);
    }

    @ApiOperation("删除乡镇预报数据")
    @GetMapping("/deleteTownshipForecast")
    public SysResult deleteTownshipForecast(String dateChar, String strTimeChar, String stationList) {
        int rows = townshipForecastService.delete(dateChar, strTimeChar, stationList);
        return SysResult.success(rows);
    }

    @ApiOperation("添加乡镇预报数据")
    @PostMapping("/saveTownshipForecast")
    public SysResult saveTownshipForecast(TownshipForecast townshipForecast) {
        int rows = townshipForecastService.save(townshipForecast);
        return SysResult.success(rows);
    }

    @ApiOperation("获取数值场数据")
    @GetMapping("/getYbzzForecastshuzhi")
    public SysResult getYbzzForecastshuzhi(String timespan, String param1, String param2, String forecastType) {
        List<YbzzForecastshuzhi> list = ybzzForecastshuzhiService.getList(timespan, param1, param2, forecastType);
        return SysResult.success(list);
    }

    @ApiOperation("删除数值场数据")
    @GetMapping("/deleteYbzzForecastshuzhi")
    public SysResult deleteYbzzForecastshuzhi(String timespan, String param1, String param2, String forecastType) {
        int rows = ybzzForecastshuzhiService.delete(timespan, param1, param2, forecastType);
        return SysResult.success(rows);
    }

    @ApiOperation("添加数值场数据")
    @GetMapping("/saveYbzzForecastshuzhi")
    public SysResult saveYbzzForecastshuzhi(YbzzForecastshuzhi ybzzForecastshuzhi) {
        Integer rows = ybzzForecastshuzhiService.save(ybzzForecastshuzhi);
        return SysResult.success(rows);
    }

    @ApiOperation("获取实况值")
    @GetMapping("/getMsgMediumSmallScale")
    public SysResult getMsgMediumSmallScale(String timespan, String param1, String param2, String forecastType) {
        List<MsgMediumSmallScale> list = msgMediumSmallScaleService.getList(timespan, param1, param2, forecastType);
        return SysResult.success(list);
    }

    @ApiOperation("插入客观订正数据")
    @PostMapping("/saveYbzzForecastkeguandz")
    public SysResult saveYbzzForecastkeguandz(YbzzForecastkeguandz ybzzForecastkeguandz) {
        int rows = ybzzForecastkeguandzService.save(ybzzForecastkeguandz);
        return SysResult.success(rows);
    }

    @ApiOperation("删除客观订正场数据")
    @GetMapping("/deleteYbzzForecastkeguandz")
    public SysResult deleteYbzzForecastkeguandz(String timespan, String param1, String param2, String forecastType) {
        int rows = ybzzForecastkeguandzService.delete(timespan, param1, param2, forecastType);
        return SysResult.success(rows);
    }

    @ApiOperation("插入差值场数据")
    @PostMapping("/saveYbzzForecastdiff")
    public SysResult saveYbzzForecastdiff(YbzzForecastdiff ybzzForecastdiff) {
        Integer rows = ybzzForecastdiffService.save(ybzzForecastdiff);
        return SysResult.success(rows);
    }

    @ApiOperation("删除差值场数据")
    @PostMapping("/deleteYbzzForecastdiff")
    public SysResult deleteYbzzForecastdiff(String timespan, String param1, String param2, String forecastType) {
        int rows = ybzzForecastdiffService.delete(timespan, param1, param2, forecastType);
        return SysResult.success(rows);
    }

    @ApiOperation("获取MappePath")
    @GetMapping("/getMappePath")
    public SysResult getMappePath() {
        List<String> mappepaths = ybzzConfigService.getMappepath();
        return SysResult.success(mappepaths);
    }

    @ApiOperation("获取权限")
    @GetMapping("/getPermissions")
    public SysResult getPermissions(String permssion) {
        List<Permissions> permissions = permissionsService.getList(permssion);
        return SysResult.success(permissions);
    }

    @ApiOperation("获取权限对应角色")
    @GetMapping("/getRoles")
    public SysResult getRoles(String permssion) {
        List<PermissionsRoles> permissionsRoles = permissionsRolesService.getList(permssion);
        return SysResult.success(permissionsRoles);
    }

    @ApiOperation("获取角色对应用户")
    @GetMapping("/getUsers")
    public SysResult getUsers(Integer roleId) {
        List<RolesUsers> rolesUsers = rolesUsersService.getList(roleId);
        return SysResult.success(roleId);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public SysResult getUserInfo(Integer userId) {
        List<Users> users = usersService.getList(userId);
        return SysResult.success(users);
    }

    @ApiOperation("获取格点文件信息")
    @GetMapping("/getGridInfo")
    public SysResult getGridInfo(String dataChar, String timeChar, String type) {
        List<GridFile> list = gridFileService.getList(dataChar, timeChar, type);
        return SysResult.success(list);
    }

    @ApiOperation("获取站点信息")
    @GetMapping("/getEnviroStation")
    public SysResult getEnviroStaiton() {
        List<EnviroStation> enviroStations = enviroStationService.getList();
        return SysResult.success(enviroStations);
    }

    @ApiOperation("获取乡镇站点信息")
    @GetMapping("/getTownshipStation")
    public SysResult getTownshipStation(String jd, String wd, String city) {
        List<TownshipStation> townshipStations = townshipStationService.getList(jd, wd, city);
        return SysResult.success(townshipStations);
    }

    @ApiOperation("获取旅游站点信息")
    @GetMapping("/getTourStations")
    public SysResult getTourStations() {
        List<TourStations> tourStations = tourStationsService.getList();
        return SysResult.success(tourStations);
    }

    @ApiOperation("获取交通气象站点信息")
    @GetMapping("/getTrafficWeatherSta")
    public SysResult getTrafficWeatherSta() {
        List<TrafficWeatherSta> trafficWeatherStas = trafficWeatherStaService.getList();
        return SysResult.success(trafficWeatherStas);
    }

    @ApiOperation("删除环保预报")
    @GetMapping("/deleteAirpolFcstQx")
    public SysResult deleteAirpolFcstQx(String forecastTime, String strTimeChar, String stationID) {
        int rows = airpolFcstQxService.delete(forecastTime, strTimeChar, stationID);
        return SysResult.success(rows);
    }

    @ApiOperation("添加环保预报")
    @GetMapping("/saveAirpolFcstQx")
    public SysResult saveAirpolFcstQx(AirpolFcstQx airpolFcstQx) {
        int rows = airpolFcstQxService.save(airpolFcstQx);
        return SysResult.success(rows);
    }

    @ApiOperation("删除旅游预报")
    @GetMapping("/deleteTourFcst")
    public SysResult deleteTourFcst(String forecastTime, String strTimeChar, String stationID) {
        int rows = tourFcstService.delete(forecastTime, strTimeChar, stationID);
        return SysResult.success(rows);
    }

    @ApiOperation("添加旅游预报")
    @PostMapping("/saveTourFcst")
    public SysResult saveTourFcst(TourFcst tourFcst) {
        TourFcst result = tourFcstService.save(tourFcst);
        if (result != null) {
            return SysResult.success(result);
        }else{
            return SysResult.fail("添加预报失败！！！");
        }
    }

    @ApiOperation("删除交通气象预报")
    @GetMapping("/deleteTrafficFcst")
    public SysResult deleteTrafficFcst(String forecastTime, String strTimeChar, String stationID) {
        int rows = trafficFcstService.delete(forecastTime, strTimeChar, stationID);
        return SysResult.success(rows);
    }

    @ApiOperation("保存交通气象预报")
    @PostMapping("/saveTrafficFcst")
    public SysResult saveTrafficFcst(TrafficFcst trafficFcst) {
        int rows = trafficFcstService.save(trafficFcst);
        return SysResult.success(rows);
    }

}
